<template>
  <div class="container" :style="{backgroundImage:'url('+headBgImgUrl+')'}">
    <div class="login-box">
      <h2 class="title" style>优趣学生信息管理系统</h2>
      <div class="tabBox">
        <div :class="['tab',isUser?'is-active':'']" @click="userTabClick">用户登录</div>
        <div :class="['tab',!isUser?'is-active':'']" @click="adminTabClick">管理员登录</div>
      </div>
      <el-form v-show="isUser" class="loginForm">
        <el-form-item>
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-s-custom"
            v-model="userLoginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            v-model="userLoginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="userLogin" type="primary" style="width:100%">登录</el-button>
        </el-form-item>
      </el-form>
      <el-form v-show="!isUser" class="loginForm">
        <el-form-item>
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-s-custom"
            v-model="adminLoginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            v-model="adminLoginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="adminLogin" type="primary" style="width:100%">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="page-footer">
      ICP证:
      <a target="_blank" class="zhen" href="http://www.beian.miit.gov.cn">浙ICP备19035240号-1</a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeName: "first",
      isUser: true,
      userLoginForm: {
        username: "",
        password: ""
      },
      adminLoginForm: {
        username: "",
        password: ""
      },
      headBgImgUrl: require("../assets/bg.jpg")
    };
  },
  methods: {
    async userLogin() {
      if (!this.userLoginForm.username) {
        this.$message.error("用户名不能为空！");
        return;
      }
      if (!this.userLoginForm.password) {
        this.$message.error("密码不能为空！");
        return;
      }
      this.userLoginForm.status = 0;
      const res = await this.$http.post("loginAPI", this.userLoginForm);
      sessionStorage.token = res.data.token;
      sessionStorage.identity = res.data.identity;
      this.$message.success("登录成功！");
      this.$router.push("/main");
    },
    async adminLogin() {
      if (!this.adminLoginForm.username) {
        this.$message.error("用户名不能为空！");
        return;
      }
      if (!this.adminLoginForm.password) {
        this.$message.error("密码不能为空！");
        return;
      }
      this.adminLoginForm.status = 1;
      const res = await this.$http.post("loginAPI", this.adminLoginForm);
      sessionStorage.token = res.data.token;
      sessionStorage.identity = res.data.identity;
      this.$message.success("登录成功！");
      this.$router.push("/main");
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    userTabClick() {
      this.adminLoginForm = {};
      this.isUser = true;
    },
    adminTabClick() {
      this.userLoginForm = {};
      this.isUser = false;
    }
  },
  created() {
    if (sessionStorage.token && sessionStorage.identity) {
      this.$router.push("/main");
    }
  }
};
</script>
<style scoped>
.container {
  padding: 0;
  margin: 0;
  height: 100vh;
  background-position: center center;
  background-size: cover;
}
.login-box {
  position: fixed;
  top: 48%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.tabBox {
  display: flex;
  align-items: center;
  background: #eee;
  margin-bottom: -1px;
}
.tab {
  width: 50%;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
}
.is-active {
  background: #eee;
}
.tab:hover {
  cursor: pointer;
}
.loginForm {
  background: #eee;
  padding: 0px 40px 30px;
  padding-top: 50px;
  border: 0;
}
.title {
  color: #fff;
  text-align: center;
  font-size: 40px;
}
.page-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right:0 ;
  padding: 10px 0;
  display: flex;
  justify-content: center;
  color: gray;
}
.zhen {
  text-decoration: none;
  color: orange;
}
</style>